﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WebNote
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            FullScreenManager.RepairWpfWindowFullScreenBehavior(this);
            /*Demo demo = new Demo();
            this.TestButton.Content = demo.GetResult();*/
        }


        private void TitlePanel_OnMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
            {

                if (this.WindowState == WindowState.Maximized)
                {
                    double width, height;
                    width = this.Width;
                    height = this.Height;

                    this.WindowState = WindowState.Normal;
                    Point ppp = Mouse.GetPosition(e.Source as FrameworkElement);
                    Point pp = (e.Source as FrameworkElement).PointToScreen(ppp);
                    Point p = new Point();
                    if (GetCursorPos(out p)) //API方法
                    {
                        this.WindowStartupLocation = WindowStartupLocation.Manual;
                        //this.Left = 0;
                        //this.Left = p.X +200;
                        this.Top = p.Y;
                        /*this.Width = width;
                        this.Height = height;*/
                    }

                }
                else
                {
                    if (e.ClickCount == 2)
                    {
                        this.WindowState = WindowState.Maximized;
                    }
                }
                this.DragMove();

            }
           
        }

        /// <summary>   
        /// 获取鼠标的坐标   
        /// </summary>   
        /// <param name="lpPoint">传址参数，坐标point类型</param>   
        /// <returns>获取成功返回真</returns>   
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern bool GetCursorPos(out Point pt);

        private bool hasEdit = true;
        private void MainWindow_OnClosing(object sender, CancelEventArgs e)
        {
            if (hasEdit)
            {
                MessageBoxResult result = MessageBox.Show("有文件正在编辑中,您真的要退出吗？", "提示", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (result == MessageBoxResult.Yes)
                {
                    e.Cancel = false;
                }
                else
                {
                    e.Cancel = true;
                }
            }
        }

        private void MinButton_OnClick(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void MaxButton_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.WindowState == WindowState.Maximized)
            {
                this.WindowState = WindowState.Normal;
            }
            else
            {
                this.WindowState = WindowState.Maximized;
            }
            
        }

        private void CloseButton_OnClick(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
       
    }
}
